<template>
  <el-form
    :model="loginForm"
    :rules="rules"
    class="login-container"
    label-position="left"
    label-width="0px"
    v-loading="loading"
  >
    <h3 class="login_title">系统登录</h3>
    <el-form-item prop="username">
      <el-input
        auto-complete="off"
        placeholder="用户名/手机号/邮箱"
        type="text"
        v-model="loginForm.username"
      ></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input
        auto-complete="off"
        placeholder="密码"
        type="password"
        v-model="loginForm.password"
      ></el-input>
    </el-form-item>
    <el-checkbox class="login_remember" label-position="left" v-model="checked"
      >记住密码</el-checkbox
    >
    <el-form-item style="width: 100%">
      <el-button
        @click.native.prevent="submitClick"
        style="width: 100%"
        type="primary"
        >登录</el-button
      >
    </el-form-item>
  </el-form>
</template>

<script>
import Crypto from '../utils/crypto';
export default {
  data() {
    var checkUsername = (rule, value, callback) => {
      if (
        value === '' ||
        value === '' ||
        value === undefined ||
        value.length < 5
      ) {
        callback(new Error('请输入正确的用户名!'));
      } else {
        callback();
      }
    };
    var checkPassword = (rule, value, callback) => {
      if (
        value === '' ||
        value === '' ||
        value === undefined ||
        value.length < 5
      ) {
        callback(new Error('请输入正确的密码!'));
      } else {
        callback();
      }
    };
    return {
      params: {},
      rules: {
        username: [
          {
            validator: checkUsername,
            trigger: 'blur'
          }
        ],
        password: [
          {
            validator: checkPassword,
            trigger: 'blur'
          }
        ]
      },
      checked: true,
      loginForm: {
        username: '',
        password: ''
      },
      loading: false
    };
  },
  methods: {
    submitClick: async function() {
      var _this = this;
      if (this.loginForm.username == '' || this.loginForm.username == null) {
        this.$messageIndex.MessageError('请输入正确用户名和密码！');
        return;
      }
      if (this.loginForm.password == '' || this.loginForm.password == null) {
        this.$messageIndex.MessageError('请输入正确用户名和密码！');
        return;
      }
      this.loginForm.password = Crypto.encryptDes(this.loginForm.password);
      let res = await _this.$api.userApi.userLogin(this.loginForm, {
        loadingTime: 100,
        loadingText: '数据加载中...'
      });
      if (res && res.code == 200) {
        let userInfo = await this.$api.userApi.userInfo();
        let userMenu = await this.$api.userApi.getMenu(this.params);
        if (userMenu.code == 200) {
          this.$util.setLocalStorage('menuInfo', userMenu.data);
        }
        this.$util.setLocalStorage('userInfo', userInfo);
        _this.currentUserName = res.username;
        _this.$router.replace({
          path: '/home'
        });
      } else {
        const confirmResult = await this.$messageIndex.MessageError(res.msg);
      }
    }
  },
  mounted: function() {
    var _this = this;
    let userInfo = this.$util.getLocalStorage('userInfo');
    if (userInfo) {
      _this.$router.replace({
        path: '/home'
      });
    }
  }
};
</script>
<style>
.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.login_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}

.login_remember {
  margin: 0px 0px 35px 0px;
  text-align: left;
}
</style>
